package xcmg.device.service.announcement;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.announcement.BasicAttachmentDO;
import xcmg.device.dao.mapper.CompanyDealerMapper;
import xcmg.device.dao.mapper.announcement.BasicAttachmentMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.announcement.BasicAttachmentVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.List;

@Service
public class BasicAttachmentService {


    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private CompanyDealerMapper companyDealerMapper;

    @Autowired
    private BasicAttachmentMapper basicAttachmentMapper;

    /**
     * 附件信息查询
     *
     * @param condition
     * @return
     */
    public PageCommonVO list(SearchCommonVO<BasicAttachmentVO> condition) {
        PageCommonVO result = new PageCommonVO();
        //按照px 排序
        PageHelperPlus.orderBy("px asc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<BasicAttachmentDO> doList = basicAttachmentMapper.list(condition.getFilters());
        List<BasicAttachmentVO> voList = new ArrayList<>();
        for (BasicAttachmentDO entity : doList) {
            BasicAttachmentVO model = new BasicAttachmentVO();
            BeanUtils.copyProperties(entity, model);
            voList.add(model);
        }
        result.setPageInfo(new PageInfo(doList));
        result.setPageInfoList(voList);
        return result;
    }

    /**
     * 新增附件信息
     * @param basicAttachmentVOList
     * @return
     */
    public ErrorCode add(List<BasicAttachmentVO> basicAttachmentVOList){
        //多个添加
        for (BasicAttachmentVO model :basicAttachmentVOList ) {
            if(null== model.getAnnouncementId() || StringUtils.isEmpty(model.getAnnouncementId())){
                return ErrorCode.AnnouncementIdIsEmpty;
            }
//            if(null== model.getValue() || StringUtils.isEmpty(model.getValue())){
//                return ErrorCode.AttachmentValueIsEmpty;
//            }
            if(null== model.getName() || StringUtils.isEmpty(model.getName())){
                return ErrorCode.AttachmentNameIsEmpty;
            }
            ActionResult<String> textResult = fastGenClient.textGuid();
            model.setId(textResult.getValue());
            int px = basicAttachmentMapper.countPxByAnnouncementId(model.getAnnouncementId());
            model.setPx(px);
            //判断结果
            if(basicAttachmentMapper.insert(model)==0){
                return ErrorCode.Failure;
            }
        }
        return ErrorCode.Success;
    }

    /**
     * 更新附件信息
     * @param model
     * @return
     */
    public ErrorCode update(BasicAttachmentVO model){
        if(basicAttachmentMapper.update(model)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    /**
     * 单条附件信息查询
     * @param id
     * @return
     */
    public BasicAttachmentVO item(String id){
        BasicAttachmentDO entity=basicAttachmentMapper.selectByPrimaryKey(id);
        if(entity==null){
            return null;
        }
        BasicAttachmentVO model=new BasicAttachmentVO();
        BeanUtils.copyProperties(entity,model);
        return model;
    }

    /**
     * 删除附件信息
     * 支持多个
     * @param ids
     * @return
     */
    public ErrorCode remove(List<String> ids){
        if(basicAttachmentMapper.updateDel(ids)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }
}
